#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2021 wanghch <wanghch@wanghch-pc>
#
# Distributed under terms of the MIT license.

"""

"""
import pandas as pd
import os
import numpy as np
from tqdm import tqdm
from talib.abstract import *
from feature_utils import *
import sys
import argparse
import datetime
import random

today = datetime.datetime.today()
parser = argparse.ArgumentParser(description='select')

ARGS = parser.parse_args()
SEQ_DAYS = [2 * i for i in range(5)]


data_dir = "data/"
export_dir = "export/"
out_dir = "out/"
datafiles = os.listdir(data_dir)
date_range = [5, 13]

industry_df = pd.read_csv("./industry.csv")
industry_id = {}
for index, row in  industry_df.iterrows():
    industry_id[row['code']] = (row["swl1_id"], row["swl2_id"], row["swl3_id"])




def macd_sign(macds, dts, code):
    last_aera = 0
    cur_aera = 0
    d_aeras = []
    g_aeras = []
    lstate = 'normal'
    sign = {}
    signs = []
    for index, d in enumerate(dts):
        sign = {}
        flag = -1
        if index > 2:
            m2 = macds[dts[index - 2]][2][code]
            m1 = macds[dts[index - 1]][2][code]
            m0 = macds[dts[index]][2][code]
            if m2 > m1 > m0:
               # bad
                flag = 1
            if m2 < m1 < m0:
               # good
                flag = 2
            if m2 < m1 and m1 > m0:
                flag = 3
            if m2 > m1 and m1 < m0:
                flag = 4

        cur_m  = macds[d][2][code]
        if cur_aera >= 0 and cur_m > 0:
            cur_aera += cur_m
        if cur_aera <= 0 and cur_m < 0:
            cur_aera += cur_m

        if cur_aera > 0 and flag == 3:
            sign['sign'] = 'buy smaller'

        if cur_aera < 0 and flag == 3:
            sign['sign'] = 'sell larger'
#         print(flag)

        last_same_aera = None
        if cur_aera > 0 and g_aeras:
            last_same_aera = g_aeras[-1]
        if cur_aera < 0 and d_aeras:
            last_same_aera =  d_aeras[-1]
#         print(d, last_same_aera, cur_aera, flag)
        state = 'normal'
        if last_same_aera:
            aera_large_flag = abs(cur_aera) > abs(last_same_aera)
            if cur_aera > 0:
                if aera_large_flag:
                    sign['bc'] = 'ok:not bc'
                else:
                    if flag == 3 or flag == 1:
                        if abs(cur_aera) * 2 < abs(last_same_aera):
                            sign['bc'] = 'warn:buy bc'
            else:
                if aera_large_flag:
                    state = 'danger:sell not bc'
                else:
                    if flag == 3 or flag == 2:
                        if abs(cur_aera) * 2 < abs(last_same_aera):
                            sign['bc'] = 'maybe good sign: bad maybe bc'
        if sign:
            signs.append(sign)
        #if state != lstate and state != 'normal':
        #    print(d, state)
        #    lstate = state
        if index > 0:
            last_m = macds[dts[index - 1]][2][code]
            if last_m < 0 and cur_m > 0:
                d_aeras.append(cur_aera)
                sign['sign'] = 'gold cross'
                last_aera = cur_aera
                cur_aera = cur_m
            if last_m > 0 and cur_m < 0:
                sign['sign'] = 'dead cross'
                g_aeras.append(cur_aera)
                last_aera = cur_aera
                cur_aera = cur_m
    return sign



code_df = pd.read_csv("./clist.csv")
for index, row in code_df.iterrows():
    code = row['code']
    name = row['display_name']
    code_file = os.path.join(data_dir, code + "_" + name + ".csv")
    df = pd.read_csv(code_file)
    print(df.count)
    break



